﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wwtfly.BBS.BackModels.Models.NewsType;
using Wwtfly.BBS.BackModels.Models.News;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using Wwtfly.DBUtility;
using System.Data.SqlClient;

namespace Wwtfly.BBS.BackDAL.News
{
    /// <summary>
    /// 新闻 DAL
    /// </summary>
    public class NewTypesDAL
    {

        /// <summary>
        /// 根据ID查询
        /// </summary>
        public static readonly string NewTypes_Select = "SELECT Id, Title, NewTime, NewState, NewRemark FROM  pm_newTypes WHERE (NewState=@NewsState or @NewsState is null)";
        
        public const string PM_NEWTYPES_SELECT_BY_ID = " SELECT [Id],[Title],[NewTime],[NewState],[NewRemark]" +
                                        " FROM [pm_newTypes]" +
                                        " WHERE (" +
                                        " [Id] = @Id)";
        /// <summary>
        /// 根据Id获取指定的NewsTypesModel
        /// </summary>
        /// <param name="Id">NewsTypesModel主键</param>
        /// <returns></returns>
        public static NewsTypesModel GetInfo(Int32 Id)
        {
            using (IDataReader dr = DBHelper.ExecuteReader(PM_NEWTYPES_SELECT_BY_ID,
                new SqlParameter() { ParameterName = "@Id", SqlDbType = SqlDbType.Int, Value = Id }
                ))
            {
                return Emit.ConvertTo<NewsTypesModel>(dr);
            }
        }
        /// <summary>
        /// 分页 获取 新闻类型
        /// </summary>
        /// <param name="newsState">新闻状态</param>
        /// <returns>List NewsTypesModel</returns>
        public static IList<NewsTypesModel> GetNewsTypesList(string newsState = "0")
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(NewTypes_Select);
            db.AddInParameter(dbCommand, "@NewsState", DbType.String, newsState);
            using (IDataReader dr = db.ExecuteReader(dbCommand))
            {
                return Emit.ConvertToList<NewsTypesModel>(dr);
            }
        }
    }
}
